package com.inpion.biz.repository.handler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.inpion.biz.dataStorage.DataResult;

public class CassandraHandler extends AbstractDataStorageHandler{

	@Override
	public DataResult execute() {
		
		DataResult dataResult = null;
		System.out.println("execute CassandraHandler ~!");

        try {
        	
        	Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
            Connection con = DriverManager.getConnection("jdbc:cassandra:/@localhost:9160/tutorials");

            PreparedStatement pstmt = con.prepareStatement(this.queryString);

            ResultSet rs = pstmt.executeQuery();
            
            System.out.println("connection : " + con);
            System.out.println("queryString : " + queryString);
            System.out.println("resultSet : " + rs);                      
            
            dataResult = buildDataResult(rs);
            
            rs.close();            
            pstmt.close();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        
		return dataResult; 
	}

	private DataResult buildDataResult(ResultSet rs) throws SQLException {
		
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		
		System.out.println("meta information : "+ rsmd);
		System.out.println("meta column count : " + columnCount);
		
		for (int i=0; i<columnCount; i++){
			String columnClassName = rsmd.getColumnClassName(i);
			String columnLabel = rsmd.getColumnLabel(i);
			String columnName = rsmd.getColumnName(i);
			int columnType = rsmd.getColumnType(i);
			String columnTypeName = rsmd.getColumnTypeName(i);
			
			String dispMeta = "[" + columnClassName + "],[" + columnLabel + "],[" + columnName + "],[" + columnType + "::" + columnTypeName + "],[";
			System.out.println(dispMeta);
			
		}
		
				
		return new DataResult();
	}


}
